<template>
  <div class="register-container">
    <el-form ref="form" :model="form" label-width="100px" class="register-form">
      <h2 class="title">用户注册</h2>
      <el-form-item label="用户名" required>
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" required>
        <el-input 
          v-model="form.password" 
          type="password" 
          placeholder="请输入密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名">
        <el-input v-model="form.real_name" placeholder="请输入真实姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleRegister">注册</el-button>
        <el-button @click="goToLogin">返回登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { register } from '@/api/user';
import { ElMessage } from 'element-plus';

const router = useRouter();
const form = ref({
  username: '',
  password: '',
  email: '',
  real_name: '',
  phone: ''
});

const handleRegister = async () => {
  if (!form.value.username || !form.value.password) {
    ElMessage.warning('用户名和密码不能为空');
    return;
  }
  
  try {
    const res = await register(form.value);
    if (res.code === 200) {
      ElMessage.success('注册成功');
      router.push('/login');
    } else {
      ElMessage.error(res.message);
    }
  } catch (error) {
    ElMessage.error('注册失败');
  }
};

const goToLogin = () => {
  router.push('/login');
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}
.register-form {
  width: 500px;
  padding: 30px;
  background: #fff;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.title {
  text-align: center;
  margin-bottom: 30px;
}
</style>